package com.baizhi.zjs.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.ServletActionContext;

import com.baizhi.zjs.dao.AddressDAO;
import com.baizhi.zjs.entity.FUSerEntity;
import com.baizhi.zjs.entity.FrontAddressEntity;
import com.baizhi.zjs.util.MybatisUtil;
import com.baizhi.zjs.util.UUIDUtil;

public class FrontAddressServieInpl implements FrontAddressServie {

@Override
	public void addAddress(FrontAddressEntity frsss) {
		System.out.println(frsss);
		HttpSession session = ServletActionContext.getRequest().getSession();
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressDAO dao = sqlSession.getMapper(AddressDAO.class);
		try {
			//从session中获取用户的id
	FUSerEntity user =(FUSerEntity) session.getAttribute("login");
	System.out.println("user的信息         "+user);
	//将用户的id存到地址对象的user_id属性中
	frsss.setUser_id(user.getId());
	//创建UUId存到地址的id中
	String uuid = UUIDUtil.getUuid();
	frsss.setId(uuid);
	System.out.println("service层的对象============"+frsss);
			dao.addAddress(frsss);
			MybatisUtil.commit(sqlSession);
		} catch (Exception e) {
			// TODO: handle exception
			MybatisUtil.rollback(sqlSession);
			e.printStackTrace();
		}
		
	}

	@Override
	public List<FrontAddressEntity> showAllAddress(String id) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressDAO mapper = sqlSession.getMapper(AddressDAO.class);
		List<FrontAddressEntity> list = mapper.showAllAddress(id);
		MybatisUtil.close(sqlSession);
		return list;
	}

	@Override
	public FrontAddressEntity sgowOneAssress(String id) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressDAO mapper = sqlSession.getMapper(AddressDAO.class);
		System.out.println(id);
		FrontAddressEntity entity = mapper.sgowOneAssress(id);
		//FrontAddressEntity entity = mapper.sgowOneAssress(id);
		System.out.println("service层======");
		System.out.println(entity);
		return entity;
	}

	@Override
	//修改地址
	public void updateAddress(FrontAddressEntity ff) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressDAO dao = sqlSession.getMapper(AddressDAO.class);
		dao.updateAddress(ff);
		MybatisUtil.commit(sqlSession);
	}

}
